package cn.wolfcode.wolf2w.util;

import cn.wolfcode.wolf2w.query.QueryObject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

public class DBHelper {

    public static <T> Page<T> query(Query query, MongoTemplate template, Class<T> clzz,
                                    Pageable pageable, QueryObject qo) {
        //查询总条数
        long total = template.count(query,clzz);
        if (total == 0){
            return Page.empty();
        }
        query.with(pageable);
        List<T> list = template.find(query,clzz);
        return new PageImpl<>(list,pageable,total);
    }

}
